package cn.wen.yinxiu.system.service.impl;

import cn.wen.yinxiu.common.enums.DelEnum;
import cn.wen.yinxiu.common.enums.StatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.wen.yinxiu.common.toolkit.PageUtils;
import cn.wen.yinxiu.common.toolkit.Query;

import cn.wen.yinxiu.system.dao.SysParamsDao;
import cn.wen.yinxiu.system.entity.SysParamsEntity;
import cn.wen.yinxiu.system.service.SysParamsService;


@Service("sysParamsService")
public class SysParamsServiceImpl extends ServiceImpl<SysParamsDao, SysParamsEntity> implements SysParamsService {

    @Autowired
    private SysParamsDao sysParamsDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysParamsEntity> page = this.page(
                new Query<SysParamsEntity>().getPage(params),
                new QueryWrapper<SysParamsEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<String> getSystemCarouselParams(String paramKey) {
        // 1. TODO 查看分布式缓存中是否存在

        // 2.数据库查找数据
        QueryWrapper<SysParamsEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("params_key", paramKey);
        queryWrapper.eq("status", StatusEnum.NORMAL_USE.code());
        queryWrapper.eq("is_deleted", DelEnum.NORMAL.code());
        SysParamsEntity sysParamsEntity = sysParamsDao.selectOne(queryWrapper);
        List<String> list = new ArrayList<>();
        if (sysParamsEntity == null) {
            return list;
        }
        // 按格式截取轮播图数据 ; 分隔数据
        String paramsValue = sysParamsEntity.getParamsValue();
        String[] split = paramsValue.split(";");
        for (String s : split) {
            list.add(s);
        }
        return list;
    }

}